package com.example.Wisdompark.controller;

import com.example.Wisdompark.dao.OutOrderMapper;
import com.example.Wisdompark.pojo.Current_Cargo;
import com.example.Wisdompark.pojo.Entry_Order;
import com.example.Wisdompark.pojo.Out_Order;
import com.example.Wisdompark.pojo.Warehouse;
import com.example.Wisdompark.service.CurrentCargoService;
import com.example.Wisdompark.service.EntryOrderService;
import com.example.Wisdompark.service.WarehouseService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

/**
 * 仓库及货物信息处理
 */

@Controller
public class WareHouseController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private CurrentCargoService currentCargoService;
    @Autowired
    private EntryOrderService entryOrderService;
    @Autowired
    private OutOrderMapper outOrderMapper;


    // 跳转到仓库情况页面
    @RequestMapping("/Warehouse")
    public String PageToWareHose(Model model) {
        // 查询所有仓库情况
        List<Warehouse> warehouses = warehouseService.quaryAll();
        // 查询所有已批准入库订单情况
        List<Entry_Order> entry_orders = entryOrderService.queryOrderByState(1);
        // 查询所有已批准出库订单情况
        List<Out_Order> out_orders = outOrderMapper.queryOrderByState(1);

        model.addAttribute("warehouses",warehouses);
        model.addAttribute("entryOrders",entry_orders);
        model.addAttribute("outOrders",out_orders);

        return "managers/warehouse";
    }

    // 获取仓库中货物信息
    @RequestMapping("/WarehouseCondition")
    public void WarehouseCondition(HttpServletResponse response) {
        List<Warehouse> warehouses = warehouseService.quaryAll();
        Gson gson = new Gson();
        String jsonList = gson.toJson(warehouses);

        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(jsonList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 货物信息
    @RequestMapping("/CargoCondition")
    public String PageToCargo(Model model) {
        List<Current_Cargo> current_cargos = currentCargoService.queryAllCargo();
        model.addAttribute("currentcargos",current_cargos);
        return "managers/cargoCondition";
    }

}
